package com.foreverwin.jzymes.production.grind.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.grind.domain.GrindCheck;
import com.foreverwin.jzymes.production.grind.domain.GrindCheckNc;
import com.foreverwin.jzymes.production.grind.domain.GrindLog;
import com.foreverwin.jzymes.production.grind.mapper.GrindLogAdviceMapper;
import com.foreverwin.jzymes.production.grind.service.IGrindCheckAdviceService;
import com.foreverwin.jzymes.production.grind.service.IGrindLogAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/production/grind")
public class GrindController {
    @Autowired
    IGrindLogAdviceService grindLogAdviceService;
    @Autowired
    IGrindCheckAdviceService grindCheckAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    GrindLogAdviceMapper grindLogAdviceMapper;

    @RequestMapping("/item")
    public AjaxResult findItemList(){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );

        List<Map<String,Object>> itemTreeList = new ArrayList<>();
        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        commonService.findItemTree( site, itemGroup, itemTreeList, true, "B" );

        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMap : itemTreeList ){

            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", itemMap.get( "ITEM_GROUP" ) );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
            rtnItemList.add( rtnItemMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    @RequestMapping("/existItem")
    public AjaxResult existItem( @RequestParam String item ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );

        Map<String,Object> itemMap = itemAdviceService.existItem( site, item );

        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );
        List<Map<String,Object>> itemTreeList = new ArrayList<>();
        commonService.findItemTree( site, itemGroup, itemTreeList, false, "B" );
        if( !itemTreeList.contains( itemMap ) ){
            throw new BusinessException( "common.itemNotValid", new String[]{ item } );
        }

        Map<String,Object> rtnItemMap = new HashMap<>();
        rtnItemMap.put( "machineType", itemGroup );
        rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
        rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
        rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
        rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
        rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
        return AjaxResult.returnSuccess().put( "value", rtnItemMap );
    }

    /**
     * 研磨产出
     *
     * @param grindLog
     * @return
     */
    @RequestMapping("/createGrindLog")
    public AjaxResult createGrindLog(@RequestBody GrindLog grindLog ){
        grindLog.setSite( CommonMethods.getSite() );
        try{
            grindLogAdviceService.createGrinLog( grindLog );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 研磨检验
     *
     * @param grindCheck
     * @param grindCheckNcs
     * @return
     */
    @RequestMapping("/grindCheck")
    @ParamBind
    public AjaxResult grindCheck( GrindCheck grindCheck, ArrayList<GrindCheckNc> grindCheckNcs ){
        grindCheck.setSite( CommonMethods.getSite() );
        try{
            grindCheckAdviceService.grindCheck( grindCheck, grindCheckNcs );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 查询研磨产出记录
     *
     * @param itemBo
     * @return
     */
    @RequestMapping("/findGrindLog")
    public AjaxResult findGrindLog( @RequestParam String itemBo ){
        String site = CommonMethods.getSite();
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        Date[] dateRange = productionShiftService.findProductionDayRange( site );
        List<GrindLog> grindLogs = grindLogAdviceMapper.selectGrindLogList( site, null, null, itemBo, dateRange[0], dateRange[1] );
        Long totalOkQty = 0L;
        List<Map<String,Object>> rtnGrindLogs = new ArrayList<>();
        for( GrindLog grindLog : grindLogs ){
            Map<String,Object> rtnGrindLog = new HashMap<>();
            rtnGrindLog.put( "machineType", grindLog.getMachineType() );
            rtnGrindLog.put( "resrce", grindLog.getResrce() );
            Map<String,Object> itemMap = itemAdviceService.existItem( grindLog.getItemBo() );
            rtnGrindLog.put( "item", itemMap.get("ITEM") );
            rtnGrindLog.put( "description", itemMap.get("DESCRIPTION") );
            rtnGrindLog.put( "okQty", grindLog.getOkQty() );
            rtnGrindLog.put( "createdDateTime", grindLog.getCreatedDateTime() );
            rtnGrindLog.put( "createUser", grindLog.getCreateUserBo() );
            rtnGrindLogs.add( rtnGrindLog );
            totalOkQty += grindLog.getOkQty();
        }
        return AjaxResult.returnSuccess().put( "value", rtnGrindLogs ).put( "totalOkQty", totalOkQty );
    }

}
